package com.zw.stock_sell_storage.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zw.stock_sell_storage.dto.SaleStatisticDto;
import com.zw.stock_sell_storage.dto.SaleStatisticMonthDto;
import com.zw.stock_sell_storage.dto.ex.QueryTimeScopeDtoEx;
import com.zw.stock_sell_storage.mapper.SaleStatisticMapper;
import com.zw.stock_sell_storage.service.ISaleStatisticService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;


@Service
public class SaleStatisticServiceImpl extends ServiceImpl<SaleStatisticMapper, SaleStatisticDto> implements ISaleStatisticService {
    @Autowired
    SaleStatisticMapper saleStatisticMapper;
    @Override
    public List<SaleStatisticDto> querySaleStatisticAsGoodsType(QueryTimeScopeDtoEx queryTimeScopeDtoEx) {
        return saleStatisticMapper.selectSaleStatisticAsGoodsType(queryTimeScopeDtoEx);
    }

    @Override
    public List<SaleStatisticDto> querySaleStatisticAsGoodsCode(QueryTimeScopeDtoEx queryTimeScopeDtoEx) {
        return saleStatisticMapper.selectSaleStatisticAsGoodsCode(queryTimeScopeDtoEx);
    }

    @Override
    public List<String> querySaleStatisticYear() {
        return saleStatisticMapper.selectSaleStatisticYear();
    }

    @Override
    public List<SaleStatisticMonthDto> querySaleStatisticAsGoodsTypeAndMonth(String year) {
        List<SaleStatisticDto> saleStatisticList = saleStatisticMapper.selectSaleStatisticAsGoodsTypeAndMonth(year);
        List<SaleStatisticMonthDto> saleStatisticMonthList = new ArrayList<>();
        for (SaleStatisticDto saleStatisticDto : saleStatisticList) {
            int index = -1;
            for (SaleStatisticMonthDto saleStatisticMonthDto : saleStatisticMonthList) {
                if (saleStatisticMonthDto.getName().equals(saleStatisticDto.getStatisticName())) {
                    saleStatisticMonthDto.getData()[Integer.parseInt(saleStatisticDto.getStatisticMonth())-1]=saleStatisticDto.getStatisticData();
                    index=0;
                    break;
                }
            }
            if (index == -1){
                SaleStatisticMonthDto saleStatisticMonthDto2 = new SaleStatisticMonthDto();
                saleStatisticMonthDto2.setData(new double[12]);
                saleStatisticMonthDto2.setType("bar");
                saleStatisticMonthDto2.setName(saleStatisticDto.getStatisticName());
                saleStatisticMonthDto2.getData()[Integer.parseInt(saleStatisticDto.getStatisticMonth())-1]=saleStatisticDto.getStatisticData();
                saleStatisticMonthList.add(saleStatisticMonthDto2);
            }
        }
        return saleStatisticMonthList;
    }

}
